<!-- 继承公共视图文件 -->
<extend name="./Application/Admin/View/layout.html" />
 <!-- 可变内容区域 -->
<block name="mid">
    <div class="layui-row">
        <div class="layui-col-md12">
            <div class="panel-heading">
                <h2><i class="fa fa-indent red"></i><strong>&nbsp;&nbsp;添加商品</strong></h2>
            </div>
        </div>
        <hr class="layui-bg-black">

        <div class="layui-col-md6 layui-col-md-offset3" style="margin-top: 30px;">
          <form class="layui-form" action="" method="post">

            <div class="layui-form-item">
                <label class="layui-form-label" style="width:100px;">商品名称</label>
                <div class="layui-input-block">
                    <input type="text" name="goods_name" placeholder="例：兰蔻/法兰琳卡" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width:100px;">市场价</label>
                <div class="layui-input-block">
                    <input type="text" name="market_price" placeholder="￥" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width:100px;">现价</label>
                <div class="layui-input-block">
                    <input type="text" name="shop_price" placeholder="￥" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width:100px;">上架</label>
                <div class="layui-input-block">
                    <input type="radio" name="is_up" value="1" title="是" checked>
                    <input type="radio" name="is_up" value="0" title="否">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width:100px;">新品</label>
                <div class="layui-input-block">
                    <input type="radio" name="is_new" value="1" title="是" checked>
                    <input type="radio" name="is_new" value="0" title="否">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width:100px;">热卖</label>
                <div class="layui-input-block">
                    <input type="radio" name="is_hot" value="1" title="是" checked>
                    <input type="radio" name="is_hot" value="0" title="否">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width:100px;">商品类型</label>
                <div class="layui-input-block" style="width:150px;">
                    <select id="goods_type" lay-verify="" lay-filter="goods_type">
                      <option value="">请选择商品类型</option>
                      <foreach name="goods_type" item="v">
                        <option value="{$v.id}">{$v.name}</option>
                      </foreach>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" style="width:100px;">商品规格项</label>
                <div class="layui-input-block spec_item">
                    <!-- <div class="layui-row">
                      <div class="layui-input-block">
                          <label class="layui-form-label" style="width:100px; margin-left:-140px">颜色</label>
                      </div>
                      <div class="layui-input-block" style="margin-left:0">
                          <div class="layui-col-md2">
                              <span class="layui-btn layui-btn-disabled" style="width:100px;"></span>
                              <button type="button" class="layui-btn layui-btn-normal" style="width:100px; margin-left: 0;">+</button>
                              <input type="text" class="layui-input" style="width:100px;" name="">
                          </div>
                      </div>
                    </div>

                    <table class="layui-table" lay-size="sm">
                        <thead height="40">
                          <tr>
                            <th width="120">规格总价</th>
                            <th width="120">库存</th>
                            <th width="400">规格描述</th>
                            <th width="50">操作</th>
                          </tr>
                        </thead>
                        <tbody>
                          <tr>
                            <td>贤心</td>
                            <td>贤心</td>
                            <td>2016-11-29</td>
                            <th>
                              <div class="layui-btn-group">
                                <button class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon"></i></button>
                              </div>
                            </th>
                          </tr>
                        </tbody>
                    </table>

                    <div class="layui-col-md1 layui-col-md-offset10">
                      <div class="grid-demo grid-demo-bg1">
                        <button class="layui-btn layui-btn-danger">规格录入</button>
                      </div>
                    </div> -->

                </div>


            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" style="width:100px;">分类类型</label>
                <div class="layui-input-block" style="width:150px;">
                    <select name="cate_id" lay-verify="">
                      <option value="">请选择分类类型</option>
                      <foreach name="cate" item="v">
                        <option value="{$v.id}">{$v.name}</option>
                      </foreach>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width:100px;">品牌类型</label>
                <div class="layui-input-block" style="width:150px;">
                    <select name="brand_id" lay-verify="">
                      <option value="">请选择品牌类型</option>
                      <foreach name="brand" item="v">
                        <option value="{$v.id}">{$v.name}</option>
                      </foreach>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" style="width:100px;">商品图片</label>
                <div class="layui-input-block">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn"  id="img">上传图片</button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="demo1">
                            <p id="demoText"></p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">

                <div class="layui-input-block">
                    <script id="editor" name="desc" type="text/plain" style="width:900px;height:300px;"></script>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                  <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                  <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
          </form>
        </div>
    </div>

</block>
<block name="js">

  <!-- jquery -->
  <script type="text/javascript" src="__ADMIN__assets/js/jquery-2.1.1.min.js"></script>
	<script type="text/javascript" src="__ADMIN__assets/js/jquery.validate.min.js"></script>
  <!-- jquery -->

  <!-- 百度编辑器is文件 -->
  <script type="text/javascript" charset="utf-8" src="__ADMIN__ueditor/ueditor.config.js"></script>
  <script type="text/javascript" charset="utf-8" src="__ADMIN__ueditor/ueditor.all.min.js"> </script>
  <!-- 百度编辑器is文件 -->

	<script type="text/javascript">
		$(function(){

      //初始化百度编辑器
      var ue = UE.getEditor('editor');

      //规格录入
      $('.spec_item').on('click','#spec_in',function(event) {


          var total_price = 0;//规格总价
          var spec_desc = '';//规格项描述
          var specItem_id = '';//规格规则
          var number = $(':text[name="number"]').val();//库存

          //抓取所有录入的规格项单价
          var price = $('.spec_item input[name="price"]');

          var inp_len = $('.spec_num').length;

          if(price.length!=inp_len){
            alert('请将规格信息填写完整');
            return false;
          }

          var numb = $('.spec_item input[name="number"]').val();

          if(numb=='' && !isNaN(numb)){
            alert('请输入库存且是数字类型');
            return false;
          }

          //循环累计规格项单价、规格项描述
          price.each(function(i,obj){
            total_price+=parseInt($(obj).val());
            spec_desc+=$(obj).prev().attr('spec-desc')+'；';
            specItem_id+='_'+$(obj).prev().attr('specItem-id');
          })
          console.log(specItem_id);
         // return false;

          $.ajax({
            type:'post',
            url:'{:U("Admin/ajax/spec_into")}',
            data:{
              'spec_rules':specItem_id,
              'number':number,
              'spec_price':total_price,
              'spec_desc':spec_desc,
            },
            datatype:'json',
            success:function(e){

              var html = '';
              html += '<div class="layui-row"><div class="layui-input-block" style="margin-left:0"><table class="layui-table" lay-size="sm">';
              html += '<thead height="40">';
              html += '<tr>';
              html += '<th width="120">规格总价</th>';
              html += '<th width="100">库存</th>';
              html += '<th width="400">规格描述</th>';
              html += '<th width="50">操作</th>';
              html += '</tr>';
              for(i in e){
                html += '<tbody>';
                html += '<tr>';
                html += '<td>'+e[i].spec_price+'</td>';
                html += '<td>'+e[i].number+'</td>';
                html += '<td>'+e[i].spec_desc+'</td>';
                html += '<th>';
                html += '<button class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon"></i></button>';
                html += '</th>';
                html += '</tbody></div></div>';
              }
              $('.spec_list').html(html);
            }
          })

      });

      //根据所选商品类型显示相应的规格项
      form.on('select(goods_type)', function(data){
          // console.log(data.elem); //得到select原始DOM对象
          // console.log(data.value); //得到被选中的值
          // console.log(data.othis); //得到美化后的DOM对象

          // 获取被选中项的商品id
          var id = data.value;

          $.ajax({
            type:'get',
            url:'{:U("Admin/ajax/getSpec")}',
            data:{
              'id':id,
            },
            datatype:'json',
            success:function(j){
              var html = '';
              for(i in j){

                html += '<div class="layui-row spec_num">';
                html += '<div class="layui-input-block">';
                html += '<label class="layui-form-label" style="width:100px; margin-left:-140px">'+j[i].spec_name+'</label>';
                html += '</div>';
                html += '<div class="layui-input-block" style="margin-left:0">';
                for(s in j[i]['spec_item']){
                html += '<div class="layui-col-md2">';
                html += '<span class="layui-btn layui-btn-primary" style="width:100px;">'+j[i]['spec_item'][s]['name']+'</span>';
                var desc = j[i].spec_name+':'+j[i]['spec_item'][s]['name'];
                html += '<button type="button" class="layui-btn layui-btn-normal add" specItem-id="'+j[i]['spec_item'][s]['id']+'" spec-desc="'+desc+'" style="width:100px; margin-left: 0;">+</button>';
                html += '</div> ';
                }
                html += '</div>';
                html += '</div>';
              }

                //追加库存
                html += '<div class="layui-row">';
                html += '<div class="layui-input-block">';
                html += '<label class="layui-form-label" style="width:100px; margin-left:-170px">库存</label>';
                html += '</div>';
                html += '<div class="layui-input-block" style="margin-left:0">';
                html += '<div class="layui-col-md2">';
                html += '<input type="text" class="layui-input" name="number" style="width:100px; border:1px solid #c9c9c9">';
                html += '</div> ';
                html += '</div>';
                html += '</div>';

                //添加规格输入按钮
                var htmlluru = html+'<div id="spec_in" class="layui-col-md1 layui-col-md-offset10"><div class="grid-demo grid-demo-bg1"><button  type="button" class="layui-btn layui-btn-danger">规格录入</button></div></div><div class="spec_list"></div>';


                $('.spec_item').html(htmlluru);

              $('.add').click(function(){

                  var icon = $(this).text();
                  if(icon=='+'){
                    //优化再次点击继续累加文本框的问题
                    $(this).parent().parent().find(':text[name="price"]').remove();
                    $(this).parent().parent().find(':button').text('+');

                    $(this).text('-');
                    var inp = '<input type="text" class="layui-input" placeholder="￥" name="price" style="width:100px; text-align:center">';
                    $(this).after(inp);
                  }else{

                    $(this).text('+');
                    $(this).next().remove();
                  }
              })
            }
          })
      });


      $('#edui1').css('z-index','900');

        //商品图片上传
        var uploadInst = upload.render({
            elem: '#img',
            url: "{:U('admin/ajax/img')}",
            before: function(obj){
              //预读本地文件示例，不支持ie8
              obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接（base64）
              });
            },
            done: function(res){
              //如果上传失败
              if(res > 0){
                return layer.msg('上传成功');
              }else{
                return layer.msg('上传失败');
              }
              //上传成功
            },
            error: function(){
              //演示失败状态，并实现重传
              var demoText = $('#demoText');
              demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
              demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
              });
            }
        })

		})
	</script>
</block>